package servlet;

import javax.servlet.*;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;

/**
 * @author 石小俊
 * @date 2023年06月30日 11:25
 */
@WebServlet(urlPatterns = "/life3",loadOnStartup = 1,initParams = {
        @WebInitParam(name = "username",value = "admin"),
        @WebInitParam(name = "password",value = "123456")
})
public class LifeThreeServlet implements Servlet {

    private transient ServletConfig config;

    @Override
    public void init(ServletConfig config) throws ServletException {
        String username = config.getInitParameter("username");
        String password = config.getInitParameter("password");
        System.out.println("username:"+username+",password:"+password);
        this.config = config;

    }

    public String getInitParameter(String name){
        return this.getServletConfig().getInitParameter(name);
    }

    @Override
    public ServletConfig getServletConfig() {
        return this.config;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("执行核心业务逻辑");
//        String username = getServletConfig().getInitParameter("username");
//        String password = getServletConfig().getInitParameter("password");
        String username = this.getInitParameter("username");
        String password = this.getInitParameter("password");
        System.out.println("username:"+username+",password:"+password);
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("销毁");
    }
}
